package elion.com.theElion.fragmentSeed;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import elion.com.elion.R;
import elion.com.theElion.utils.ImageLoder;
import elion.vo.seed.Crop;

/**
 * Created by Alex on 2017/7/15.
 */

public class CropAdapter extends BaseAdapter {


    private List<Crop> crops = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;


    public CropAdapter(List<Crop> crops, Context context) {
        this.crops = crops;
        System.out.println("la" + crops.size());
        this.context = context;
        this.layoutInflater = layoutInflater.from(context);

    }

    public CropAdapter() {
    }

    @Override
    public int getCount() {
        return crops.size();
    }

    @Override
    public Object getItem(int position) {
        return crops.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View mView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (mView == null) {
            mView = layoutInflater.inflate(R.layout.listview_seed_item, null);

            viewHolder = new ViewHolder();

            viewHolder.imageView = (ImageView) mView.findViewById(R.id.iv_seed_image);
            viewHolder.text = (TextView) mView.findViewById(R.id.iv_seed_title);

            mView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) mView.getTag();
        }
        String url = crops.get(position).getImg_url();
        new ImageLoder().showImageByThread(viewHolder.imageView, url);
        viewHolder.text.setText(crops.get(position).getName());

        return mView;
    }

    class ViewHolder {
        ImageView imageView;
        TextView text;

        public ImageView getImageView() {
            return imageView;
        }

        public void setImageView(ImageView imageView) {
            this.imageView = imageView;
        }

        public TextView getText() {
            return text;
        }

        public void setText(TextView text) {
            this.text = text;
        }
    }
}
